package com.siwei.apply.service.impl;

import com.siwei.apply.domain.YdbpData;
import com.siwei.apply.domain.vo.YdbpDataFilterVo;
import com.siwei.apply.domain.vo.YdbpDataUpdateVo;
import com.siwei.apply.domain.vo.YdbpDataVo;
import com.siwei.apply.mapper.YdbpDataMapper;
import com.siwei.apply.service.YdbpDataService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.siwei.apply.common.Common.UserId;

@Service
public class YdbpDataImpl implements YdbpDataService {
    @Autowired
    private YdbpDataMapper ydbpDataMapper;

    @Override
    public String add(YdbpDataVo ydbpDataVo) {
        YdbpData entity = new YdbpData();
        BeanUtils.copyProperties(ydbpDataVo, entity);
        // 生成主键ID
        entity.generateId();
        entity.setCreatorId(UserId);
        ydbpDataMapper.add(entity);
        return entity.getId();
    }

    @Override
    public YdbpData getById(String id) {
        return ydbpDataMapper.getById(id);
    }

    @Override
    public Map<String, Object> getList(YdbpDataFilterVo filterVo) {
        int pageNum = filterVo.getPageNum() == null ? 1 : filterVo.getPageNum();
        int pageSize = filterVo.getPageSize() == null ? 10 : filterVo.getPageSize();
        int offset = (pageNum - 1) * pageSize;
        List<YdbpData> rows = ydbpDataMapper.selectPage(offset, pageSize);
        int total = ydbpDataMapper.countAll();
        Map<String, Object> result = new HashMap<>();
        result.put("rows", rows);
        result.put("total", total);
        return result;
    }

    @Override
    public void update(YdbpDataUpdateVo ydbpDataVo) {
        YdbpData entity = new YdbpData();
        BeanUtils.copyProperties(ydbpDataVo, entity);
        entity.setUpdatedAt(new java.util.Date());
        ydbpDataMapper.update(entity);
    }
}
